package com.lmk.basic.controller;

import com.lmk.basic.dto.SmsCodeDto;
import com.lmk.basic.exception.BusinessException;
import com.lmk.basic.service.IVerifyCodeService;
import com.lmk.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

// 验证码接口类  /verifyCode/image/
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService iVerifyCodeService;



    @GetMapping("/image/{key}")
    public AjaxResult image(@PathVariable("key") String key){
        try {
            String base64ImageCode = iVerifyCodeService.image(key);
            return AjaxResult.me().setResultObj(base64ImageCode);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"获取失败");
        }
    }

    @PostMapping("/smsCode")
    public AjaxResult sendSmsCode(@RequestBody SmsCodeDto smsCodeDto){
        try {
            iVerifyCodeService.sendSmsCode(smsCodeDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统异常,请稍后重试!");
        }
    }

    @PostMapping("/binderSmsCode")
    public AjaxResult binderSmsCode(@RequestBody SmsCodeDto smsCodeDto){
        try {
            iVerifyCodeService.binderSmsCode(smsCodeDto);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false,e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统异常,请稍后重试!");
        }
    }
}
